package com.cea.papyrus.core.ui.dialogs;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.utils.Debug;

public class PaletteChooseSetMetaclassDialog extends ChooseSetMetaclassDialog {

	/**
	 * Constructor
	 * @param parentShell
	 * @param profile
	 * @param retrieveImported
	 */
	public PaletteChooseSetMetaclassDialog(Shell parentShell, Profile profile, boolean retrieveImported){
		super(parentShell, profile, false);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.ChooseSetMetaclassDialog#applyModifications()
	 */
	protected void applyModifications() {
		EList metaclassSet= profile.getReferencedMetaclasses();
		
		// imports selected elements that are not already imported
		for(int i=0;i<selectedElementList.getElements().size();i++){
			if(!metaclassSet.contains(selectedElementList.getElements().get(i))){
				Debug.debug(this, "To import: "+selectedElementList.getElements().get(i), channel);
				//ElementImport ei=themodel.createMetaclassReference((PackageableElement)metaclassesreference.get(i)); ca ne focntionne plus!!
				ElementImport ei= UMLFactory.eINSTANCE.createElementImport();				
				ei.setImportedElement((PackageableElement)selectedElementList.getElements().get(i));
				ei.setAlias(((NamedElement)selectedElementList.getElements().get(i)).getName());
				//themodel.getPackagedElements().add((PackageableElement)metaclassesreference.get(i));
				profile.getMetaclassReferences().add(ei);
			}
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public ArrayList<ElementImport> getSelectedElementImports() {
		ArrayList<ElementImport> elementImports = new ArrayList<ElementImport>();
		
		Iterator<Element> iter = selectedElementList.getElements().iterator();
		while(iter.hasNext()){
			elementImports.add(profile.getElementImport((PackageableElement)iter.next()));
		}
	
		return elementImports;
	}
	
}
